﻿@using KPIS.GERP.Models.ViewModel
@model IEnumerable<KPIS.GERP.Models.ICM.DeliveryNoteItem>
@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
	ViewBag.Title = "รายการตรวจรับ";
	ViewBag.Descr = "ข้อมูลรายการตรวจรับ";
}
@using (Html.BeginForm("DeleteAll", "DeliveryNoteItem", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.AntiForgeryToken()

    <div class="row-fluid">    
        <div class="block-button-menu-above">
            @if (ViewBag.bEditable)
            {
                @Html.ButtonDeleteAll()
            }
        </div>

        @Html.ValidationSummary(true, "", new {@class = "alert alert-error"})

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                    @ViewBag.Title
                </div>
                @Html.Partial("_ExportButton")
            </div>
        </div>
        <table id="table"  class ="table table-striped table-bordered table-hover"  aria-describedby="table_info">
            <thead >
                <tr>
                    <th class="center"><label ><input type ="checkbox" /><span class ="lbl"></span></label></th>
                    <th>เป็นงานระหว่างทำ</th>
                    
                    <th class="center">แหล่งเงิน</th>
                    <th class="center">รายการพัสดุ</th>
                    <th>รายละเอียด</th>
                    <th>รหัสกิจกรรม</th>
                    <th >จำนวนตามกำหนด</th>
                    <th >จำนวนตรวจรับ</th>
                    @*<th >หน่วย</th>*@
                    <th >ราคาต่อหน่วยรวมภาษี</th>
                    <th >Vat</th>
                    <th >ราคาสุทธิตรวจรับ</th>
                    <th></th>
                    @* <th class="center" style="width:80px;"><!-- Empty head column for export buttons --></th>*@
                </tr>					
            </thead>                  @{
            GERPContext db = new GERPContext();
            var pprItems = db.PrePurchaseRequestItems.Include("ProjectOperationExpense.ProjectExpense.MasBudgetSource")
                        .Include("ProjectOperationExpense.ProjectExpense.Project").ToList();
            var projectService = new KPIS.GERP.WEB.GIMS.ProjectOperationService.ProjectOperationServiceClient();
            } 
            <tbody> 
                @foreach (var m in Model)
                {
                    <tr>
                        <td class="center">
                            <label>
                                <input type ="checkbox" name="deleteChk" value ="@m.DnItemId" />
                                <span class="lbl"></span>
                            </label>
                        </td> 
                        <td>
                            <label>
                                <input type="checkbox" id="chkHaveItem@(m.DnItemId)" checked="@m.HaveItem" onchange="onHaveItemChange('@(m.DnItemId)')" @(ViewBag.bEditable ? "" : "disabled") />
                                <span class="lbl"></span>
                            </label>
                        </td>
                        <td>
                            @{

                    var pprItem = pprItems.FirstOrDefault(o => o.PrItemId == m.PoInstallItem.BidderQuotation.PrItemId);
                    if (pprItem != null &&
                        pprItem.ProjectOperationExpense != null &&
                        pprItem.ProjectOperationExpense.ProjectExpense != null &&
                        pprItem.ProjectOperationExpense.ProjectExpense.MasBudgetSource != null)
                    {
                        @:@pprItem.ProjectOperationExpense.ProjectExpense.MasBudgetSource.Name
                                }
                            }
                        </td>
                         
                        <td>
                            @if (string.IsNullOrWhiteSpace(Html.DisplayFor(model => m.PoInstallItem.BidderQuotation.PurchaseRequestItem.MasResourceItem).ToHtmlString()))
                            {
                                
                                @Html.DisplayFor(model => m.PoInstallItem.BidderQuotation.PurchaseRequestItem.Descr) 
                                @Html.Raw("&nbsp;<span class='red'>(ไม่ระบุพัสดุที่ซื้อ/จ้างทำ)</span>")
                            }
                            else
                            {
                                @Html.DisplayFor(model =>
                                    m.PoInstallItem.BidderQuotation.PurchaseRequestItem.MasResourceItem.Name).ToHtmlString()
                            }
                        </td>
                        <td>
                            @Html.DisplayFor(model => m.Descr)
                        </td>
                        <td>
                            @{
                            int ProjId =
                                pprItem.ProjectOperationExpense.ProjectExpense.Project.ProjId;
                            Decimal TotalAmount =
                                projectService.SelectProjectExpense(new ProjectExpense { ProjId = ProjId }, null)
                                    .ToList()
                                    .Sum(pe => pe.TotalAmount) ?? 0;
                            string StrBgmName =
                                pprItem.ProjectOperationExpense.ProjectExpense.Project.Name;
                            string[] StrC = StrBgmName.Split(')');
                            string BgmName = StrC[0];
                            string BgmNo = StrC.Count() > 1 ? StrC[1] : "";

                                if (!string.IsNullOrWhiteSpace(BgmNo))
                                {
                                    @:@BgmNo
                                }
                                else if(StrBgmName.Split(new[]{"รหัสกิจกรรม"}, StringSplitOptions.None).Count() >= 2)
                                {
                                    @:@StrBgmName.Split(new[]{"รหัสกิจกรรม"}, StringSplitOptions.None)[1]
                                }
                            }

                        </td>
                        <td style="text-align: right;">
                            @((m.PoInstallItem.Qty ?? 0).ToString("#,##0")) @Html.DisplayFor(model => m.PoInstallItem.BidderQuotation.PurchaseRequestItem.MasResourceItem.MasUnit.Name)
                        </td>
                        <td style="text-align: right;">
                            @((m.PoInstallItem.Qty ?? 0).ToString("#,##0")) @Html.DisplayFor(model => m.PoInstallItem.BidderQuotation.PurchaseRequestItem.MasResourceItem.MasUnit.Name)
                        </td>
                        @*<td style="text-align: center;">
                            @Html.DisplayFor(model => m.PoInstallItem.BidderQuotation.PurchaseRequestItem.MasResourceItem.MasUnit.Name)
                        </td>*@
                        <td style="text-align: right;">
                            @((m.PoInstallItem.ItemPriceIncludeVat ?? 0).ToString("#,##0.00"))
                        </td>
                        <td style="text-align: right;">
                            @((m.PoInstallItem.VatAmount ?? 0).ToString("#,##0.00"))
                        </td>
                        <td style="text-align: right;">
                            @((m.PoInstallItem.NetAmount ?? 0).ToString("#,##0.00"))
                        </td>
                        <td>
                            @*@if (m.RecordStatus != "A" && m.RecordStatus != "P")
                            {*@
                                @Html.ButtonEdit(new { id = m.DnItemId }, true)@*, PoId=m.PoId, PoInstallId=m.PoInstallId*@
                            @*}*@
                        </td>
                        @* 
                <td class="td-actions">
                    @Html.ActionLink("ตรวจรับรายการพัสดุ", "_EditorForm", new { PoInstallItemId=ViewBag.PoInstallId}
                    , new { @class="btn btn-small btn-success button-standard-width", role="button" })
                </td>*@
                    </tr>
                }
            </tbody>
        </table> 
    </div>
    
    <div style="font-size:large;color:blue;text-align:right;padding-right:7em;">
        ยอดรวม = @((Model.Sum(o => o.PoInstallItem.NetAmount) ?? 0).ToString("#,##0.00")) บาท
    </div>
}
  @section scripts {
    
	<script type="text/javascript">
		$(function () {
			var oTable1 = $('#table').dataTable({
				"aoColumns": [
                    { "bSortable": false },
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    //null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
				]
			});

			$('table th input:checkbox').on('click', function () {
				var that = this;
				$(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
					this.checked = that.checked;
					$(this).closest('tr').toggleClass('selected');
				});

			});

			$('[data-rel=tooltip]').tooltip();

			$("form").submit(function () {
				var currentForm = this;
				bootbox.confirm(function (result) {
					var _result = result;
					if (result) {
						$.ajax({
						    url: "DeleteAll",
							type: "post",
							dataType: "json",
							data: $(currentForm).serialize(),
							success: function (result) {
								if (result.success) {
									location.reload();
								}
								else {
								}
							}
						});
					}
				})
				return false;

			});

		})

		function onHaveItemChange(DnItemId) {
		    var chk = $('#chkHaveItem' + DnItemId);
		    $.ajax({
		        url: "@Url.Action("UpdateHaveItem")?DnItemId=" + DnItemId + "&HaveItem=" + chk.is(':checked'),
		        type: "post",
		        dataType: "json"
		    });
		}
	</script>

}
        

